﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * Command Stack.  A LIFO queue for Commands.
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FileSystem;
using FileSystem.Utility;

namespace FileSystem.Impl
{
    class CommandStack : ICommandStack
    {
        private List<ICommand> stack;

        public CommandStack()
        {
            stack = new List<ICommand>();
        }

        public int size()
        {
            return stack.Count();
        }

        public bool isEmpty()
        {
            return stack.Count() == 0;
        }

        public void push(ICommand c)
        {
            if (null == c)
            {
                return;
            }
            stack.Add(c);
        }

        public ICommand pop()
        {
            int index = size() - 1;
            ICommand rc = stack[index];
            stack.RemoveAt(index);
            return rc;
        }

        public void clear()
        {
            stack.Clear();
        }

        public String describe(IPrintContext context)
        {
            StringBuilder rc = new StringBuilder();
            rc.Append(context.getFormattedString("Stack has " + size() + " items:"));
            context.increaseIndent();

            for (int i = 0; i < size(); i++)
            {
                rc.Append(context.getFormattedString("Item " + i + ":"));
                context.increaseIndent();
                rc.Append(stack[i].describe(context));
                context.decreaseIndent();
            }

            context.decreaseIndent();
            return rc.ToString();
        }
    }
}
